﻿//***********************************************************************
// Assembly         : LatestMediaHandler
// Author           : cul8er
// Created          : 05-09-2010
//
// Last Modified By : cul8er
// Last Modified On : 10-05-2010
// Description      : 
//
// Copyright        : Open Source software licensed under the GNU/GPL agreement. Thanks to MediaPortal that created many of the functions used here.
//***********************************************************************

using System.Globalization;
namespace LatestMediaHandler
{
    extern alias RealNLog;
    using MediaPortal.Configuration;
    using MediaPortal.Database;
    using MediaPortal.GUI.Library;
    using MediaPortal.Music.Database;
    using MediaPortal.Picture.Database;
    using MediaPortal.TagReader;
    using MediaPortal.Utils;
    using MediaPortal.Util;
    using RealNLog.NLog;
    using SQLite.NET;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using MediaPortal.Profile;
    using MediaPortal.Services;
    using MediaPortal.Threading;
    using MediaPortal.Dialogs;
    

    /// <summary>
    /// Class handling all database access.
    /// </summary>
    class LatestMusicHandler
    {
        #region declarations
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private readonly object lockObject = new object();
        private MusicDatabase m_db = null;
        private bool isInitialized/* = false*/;
        internal Hashtable latestMusicAlbums;
        private MediaPortal.Playlists.PlayListPlayer playlistPlayer;
        private ArrayList m_Shares = new ArrayList();
        private bool _run;
        private bool _reorgRunning;
        //private MusicDatabaseSettings setting;
        private bool _usetime;
        private bool _stripArtistPrefixes;
        public Hashtable artistsWithImageMissing;
        private GUIFacadeControl facade = null;
        private ArrayList al = new ArrayList();
        internal ArrayList images = new ArrayList();
        internal ArrayList imagesThumbs = new ArrayList();    
        private int selectedFacadeItem1 = -1;
        private int selectedFacadeItem2 = -1;
        private int showFanart = 1;
        private bool needCleanup = false;
        private int needCleanupCount = 0;
        private int lastFocusedId = 0;
        #endregion

        public int LastFocusedId
        {
            get { return lastFocusedId; }
            set { lastFocusedId = value; }
        }

        public int NeedCleanupCount
        {
            get { return needCleanupCount; }
            set { needCleanupCount = value; }
        }

        public bool NeedCleanup
        {
            get { return needCleanup; }
            set { needCleanup = value; }
        }

        public int ShowFanart
        {
            get { return showFanart; }
            set { showFanart = value; }
        }

        public int SelectedFacadeItem2
        {
            get { return selectedFacadeItem2; }
            set { selectedFacadeItem2 = value; }
        }

        public int SelectedFacadeItem1
        {
            get { return selectedFacadeItem1; }
            set { selectedFacadeItem1 = value; }
        }

        public ArrayList Images
        {
            get { return images; }
            set { images = value; }
        }

        public ArrayList Al
        {
            get { return al; }
            set { al = value; }
        }

        public GUIFacadeControl Facade
        {
            get { return facade; }
            set { facade = value; }
        } 
        
        internal LatestMusicHandler()
        {
            InitDB();
            artistsWithImageMissing = new Hashtable();
        }

        internal bool IsInitialized
        {
            get { return isInitialized; }
            set { isInitialized = value; }
        }
        
        /// <summary>
        /// Initiation of the DatabaseManager.
        /// </summary>
        internal void InitDB()
        {
            try
            {
                m_db = MusicDatabase.Instance;
                IsInitialized = true;
            }
            catch (Exception e)
            {
                logger.Error("InitDB: "+ e.ToString());
            }
        }

        internal void DoScanMusicShares()
        {
            logger.Info("Scanning music collection for new tracks - starting");
            try
            {
                _run = true;
                Work work = new Work(new DoWorkHandler(this.Run));
                work.ThreadPriority = ThreadPriority.Lowest;
                work.Description = "MusicDBReorg Thread";
                GlobalServiceProvider.Get<IThreadPool>().Add(work, QueuePriority.Low);
            }
            catch
            { }            
        }

        private void Run()
        {
            while (_run)
            {                
                // Start the Music DB Reorganization
                _reorgRunning = true;

                try
                {
                    LoadShares();
                    if (_usetime)
                    {
                        m_db.MusicDatabaseReorg(m_Shares);//, setting);                        
                    }
                }
                catch
                {
                }

                if (_usetime)
                {
                    // store last run
                    using (Settings writer = new MPSettings())
                    {
                        writer.SetValue("musicdbreorg", "lastrun", DateTime.Now.Day);
                    }
                }
                _reorgRunning = false;
                _run = false;
                              
            }            
            GetLatestMediaInfo(false);            
            logger.Info("Scanning music collection for new tracks - done");
        }

    

        private int LoadShares()
        {
            m_Shares.Clear();
            using (Settings xmlreader = new MPSettings())
            {
                _usetime = xmlreader.GetValueAsBool("musicfiles", "updateSinceLastImport", true);
                _stripArtistPrefixes = xmlreader.GetValueAsBool("musicfiles", "stripartistprefixes", false);
                if (_usetime)
                {                                     
                    for (int i = 0; i < MediaPortal.Util.VirtualDirectory.MaxSharesCount; i++)
                    {
                        string strSharePath = String.Format("sharepath{0}", i);
                        string shareType = String.Format("sharetype{0}", i);
                        string shareScan = String.Format("sharescan{0}", i);

                        string ShareType = xmlreader.GetValueAsString("music", shareType, string.Empty);
                        if (ShareType == "yes")
                        {
                            continue; // We can't monitor ftp shares
                        }

                        bool ShareScanData = xmlreader.GetValueAsBool("music", shareScan, true);
                        if (!ShareScanData)
                        {
                            continue;
                        }

                        string SharePath = xmlreader.GetValueAsString("music", strSharePath, string.Empty);

                        if (SharePath.Length > 0)
                        {
                            m_Shares.Add(SharePath);
                        }
                    }
                }
            }
            return 0;
        }

        internal void GetLatestMediaInfo(bool _onStartUp)
        {
            try
            {
                int sync = Interlocked.CompareExchange(ref LatestMediaHandlerSetup.SyncPointMusicUpdate, 1, 0);
                if (sync == 0)
                {
                    int z = 1;
                    artistsWithImageMissing = new Hashtable();
                    string windowId = GUIWindowManager.ActiveWindow.ToString();
                    if (LatestMediaHandlerSetup.LatestMusic.Equals("True", StringComparison.CurrentCulture) && !(windowId.Equals("987656", StringComparison.CurrentCulture) || windowId.Equals("504", StringComparison.CurrentCulture) || windowId.Equals("501", StringComparison.CurrentCulture) || windowId.Equals("500", StringComparison.CurrentCulture)))
                    {
                        //Music
                        try
                        {
                            LatestMediaHandler.LatestsCollection hTable = GetLatestMusic(_onStartUp, LatestMediaHandlerSetup.LatestMusicType);
                            for (int i = 0; i < 3; i++)
                            {
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".thumb", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".artist", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".album", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".dateAdded", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".fanart", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".genre", string.Empty);
                                //OLD
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".thumb", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".artist", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".album", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".dateAdded", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart1", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart2", string.Empty);
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".genre", string.Empty);
                                z++;
                            }
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest.enabled", "false");
                            //OLD
                            LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest.enabled", "false");
                            if (hTable != null)
                            {
                                /*
                                for (int i = 0; i < hTable.Count && i < 3; i++)
                                {
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".thumb", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".artist", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".album", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".dateAdded", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".fanart", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".genre", string.Empty);
                                    //OLD
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".thumb", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".artist", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".album", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".dateAdded", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart1", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart2", string.Empty);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".genre", string.Empty);
                                    z++;
                                }*/
                                z = 1;
                                for (int i = 0; i < hTable.Count && i < 3; i++)
                                {
                                    logger.Info("Updating Latest Media Info: Latest music album " + z + ": " + hTable[i].Artist + " - " + hTable[i].Album);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".thumb", hTable[i].Thumb);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".artist", hTable[i].Artist);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".album", hTable[i].Album);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".dateAdded", hTable[i].DateAdded);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".fanart", hTable[i].Fanart);
                                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest" + z + ".genre", hTable[i].Genre);
                                    //OLD
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".thumb", hTable[i].Thumb);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".artist", hTable[i].Artist);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".album", hTable[i].Album);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".dateAdded", hTable[i].DateAdded);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart1", hTable[i].Fanart);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".fanart2", hTable[i].Fanart);
                                    LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest" + z + ".genre", hTable[i].Genre);
                                    z++;
                                }
                                hTable.Clear();
                                LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.latest.enabled", "true");
                                //OLD
                                LatestMediaHandlerSetup.SetProperty("#fanarthandler.music.latest.enabled", "true");
                            }
                            hTable = null;
                            z = 1;
                            
                        }
                        catch (Exception ex)
                        {
                            logger.Error("GetLatestMediaInfo (Music): " + ex.ToString());
                        }
                    }
                    else
                    {
                        LatestMediaHandlerSetup.EmptyLatestMediaPropsMusic();
                    }
                    LatestMediaHandlerSetup.SyncPointMusicUpdate = 0;
                }
            }
            catch (Exception ex)
            {
                logger.Error("GetLatestMediaInfo (Music): " + ex.ToString());
                LatestMediaHandlerSetup.SyncPointMusicUpdate = 0;
            }
        }

        internal void MyContextMenu()
        {
            try
            {                                        
                GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_MENU);
                if (dlg == null) return;

                dlg.Reset();
                dlg.SetHeading(924);

                //Play Menu Item
                GUIListItem pItem = new GUIListItem();
                pItem.Label = Translation.Play;
                pItem.ItemId = 1;
                dlg.Add(pItem);

                //Add Artist Details Menu Item
                pItem = new GUIListItem();
                pItem.Label = Translation.ArtistDetails;
                pItem.ItemId = 2;
                dlg.Add(pItem);

                //Add Album Details Menu Item
                pItem = new GUIListItem();
                pItem.Label = Translation.AlbumDetails;
                pItem.ItemId = 3;
                dlg.Add(pItem);

                //Add filter menu item "Latest Added Music", "Most Played Music", "Latest Played Music"
                if (LatestMediaHandlerSetup.LatestMusicType.Equals("Latest Added Music"))
                {
                    LatestMediaHandlerSetup.LatestMusicType = "Most Played Music";    
                    pItem = new GUIListItem();
                    pItem.Label = "Display Most Played Music";
                }
                else if (LatestMediaHandlerSetup.LatestMusicType.Equals("Most Played Music"))
                {
                    LatestMediaHandlerSetup.LatestMusicType = "Latest Played Music";    
                    pItem = new GUIListItem();
                    pItem.Label = "Display Latest Played Music";
                }
                else if (LatestMediaHandlerSetup.LatestMusicType.Equals("Latest Played Music"))
                {
                    LatestMediaHandlerSetup.LatestMusicType = "Latest Added Music";    
                    pItem = new GUIListItem();
                    pItem.Label = "Display Latest Added Music";
                }
                pItem.ItemId = 4;
                dlg.Add(pItem);
                
               
                //Show Dialog
                dlg.DoModal(GUIWindowManager.ActiveWindow);              

                if (dlg.SelectedLabel < 0)
                {
                    return;

                }

                switch (dlg.SelectedId)
                {
                    case 1:
                        {
                            GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                            GUIControl gc = gw.GetControl(919199970);
                            facade = gc as GUIFacadeControl;
                            if (facade != null)
                            {
                                PlayMusicAlbum(facade.SelectedListItem.ItemId);
                            }
                            break;
                        }                        
                    case 2:
                        {
                            GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                            GUIControl gc = gw.GetControl(919199970);
                            facade = gc as GUIFacadeControl;
                            if (facade != null)
                            {
                                ShowArtistInfo(facade.SelectedListItem.Label, facade.SelectedListItem.Label2);
                            }
                            break;
                        }                        
                    case 3:
                        {
                            GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                            GUIControl gc = gw.GetControl(919199970);
                            facade = gc as GUIFacadeControl;
                            if (facade != null)
                            {
                                ShowAlbumInfo(GUIWindowManager.ActiveWindow, false, facade.SelectedListItem.Label, facade.SelectedListItem.Label2, (facade.SelectedListItem.MusicTag as MusicTag).FileName, facade.SelectedListItem.MusicTag as MusicTag);
                            }
                            break;
                        }
                    case 4:
                        {                            
                            GetLatestMediaInfo(false);                            
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                logger.Error("MyContextMenu: " + ex.ToString());
            }
        }

        public void ShowAlbumInfo(int parentWindowID, bool isFolder, string artistName, string albumName, string strPath,
                              MusicTag tag)
        {
            try
            {
                GUIDialogOK pDlgOK = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
                GUIDialogProgress dlgProgress =
                  (GUIDialogProgress)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_PROGRESS);

                bool bDisplayErr = false;
                AlbumInfo album = new AlbumInfo();
                MusicAlbumInfo albumInfo = new MusicAlbumInfo();
                if (m_db.GetAlbumInfo(albumName, artistName, ref album))
                {
                    albumInfo.Set(album);
                }
                else
                {
                    if (null != pDlgOK && !Win32API.IsConnectedToInternet())
                    {
                        pDlgOK.SetHeading(703);
                        pDlgOK.SetLine(1, 703);
                        pDlgOK.SetLine(2, string.Empty);
                        pDlgOK.DoModal(GUIWindowManager.ActiveWindow);
                        return;
                    }

                    // show dialog box indicating we're searching the album
                    if (dlgProgress != null)
                    {
                        dlgProgress.Reset();
                        dlgProgress.SetHeading(185);
                        dlgProgress.SetLine(1, albumName);
                        dlgProgress.SetLine(2, artistName);
                        dlgProgress.SetLine(3, tag.Year.ToString());
                        dlgProgress.SetPercentage(0);
                        //dlgProgress.StartModal(GetID);
                        dlgProgress.StartModal(parentWindowID);
                        dlgProgress.ShowProgressBar(true);
                        dlgProgress.Progress();
                    }

                    // find album info
                    AllmusicSiteScraper scraper = new AllmusicSiteScraper();
                    if (scraper.FindAlbumInfo(albumName, artistName, tag.Year))
                    {
                        if (dlgProgress != null)
                        {
                            dlgProgress.SetPercentage(30);
                            dlgProgress.Progress();
                            dlgProgress.Close();
                        }
                        // Did we find multiple albums?
                        int iSelectedAlbum = 0;
                        if (scraper.IsMultiple())
                        {
                            string szText = GUILocalizeStrings.Get(181);
                            GUIDialogSelect pDlg = (GUIDialogSelect)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_SELECT);
                            if (null != pDlg)
                            {
                                pDlg.Reset();
                                pDlg.SetHeading(szText);
                                foreach (MusicAlbumInfo foundAlbum in scraper.GetAlbumsFound())
                                {
                                    pDlg.Add(string.Format("{0} - {1}", foundAlbum.Title, foundAlbum.Artist));
                                }
                                pDlg.DoModal(parentWindowID);

                                // and wait till user selects one
                                iSelectedAlbum = pDlg.SelectedLabel;
                                if (iSelectedAlbum < 0)
                                {
                                    return;
                                }
                            }
                            // ok, now show dialog we're downloading the album info
                            MusicAlbumInfo selectedAlbum = scraper.GetAlbumsFound()[iSelectedAlbum];
                            if (null != dlgProgress)
                            {
                                dlgProgress.Reset();
                                dlgProgress.SetHeading(185);
                                dlgProgress.SetLine(1, selectedAlbum.Title2);
                                dlgProgress.SetLine(2, selectedAlbum.Artist);
                                dlgProgress.StartModal(parentWindowID);
                                dlgProgress.ShowProgressBar(true);
                                dlgProgress.SetPercentage(40);
                                dlgProgress.Progress();
                            }

                            if (!scraper.FindInfoByIndex(iSelectedAlbum))
                            {
                                if (null != dlgProgress)
                                {
                                    dlgProgress.Close();
                                }
                                return;
                            }
                        }

                        if (null != dlgProgress)
                        {
                            dlgProgress.SetPercentage(60);
                            dlgProgress.Progress();
                        }

                        // Now we have either a Single hit or a selected Artist
                        // Parse it
                        if (albumInfo.Parse(scraper.GetHtmlContent()))
                        {
                            if (null != dlgProgress)
                            {
                                dlgProgress.SetPercentage(80);
                                dlgProgress.Progress();
                            }
                            // set album title and artist from musicinfotag, not the one we got from allmusic.com
                            albumInfo.Title = albumName;
                            albumInfo.Artist = artistName;

                            // set path, needed to store album in database
                            albumInfo.AlbumPath = strPath;
                            album = new AlbumInfo();
                            album.Album = albumInfo.Title;
                            album.Artist = albumInfo.Artist;
                            album.Genre = albumInfo.Genre;
                            album.Tones = albumInfo.Tones;
                            album.Styles = albumInfo.Styles;
                            album.Review = albumInfo.Review;
                            album.Image = albumInfo.ImageURL;
                            album.Rating = albumInfo.Rating;
                            album.Tracks = albumInfo.Tracks;
                            try
                            {
                                album.Year = Int32.Parse(albumInfo.DateOfRelease);
                            }
                            catch (Exception) { }

                            // save to database
                            m_db.AddAlbumInfo(album);
                            if (null != dlgProgress)
                            {
                                dlgProgress.SetPercentage(100);
                                dlgProgress.Progress();
                                dlgProgress.Close();
                            }

                            if (isFolder)
                            {
                                // if there's an album thumb
                                string thumb = MediaPortal.Util.Utils.GetAlbumThumbName(albumInfo.Artist, albumInfo.Title);
                                // use the better one
                                thumb = MediaPortal.Util.Utils.ConvertToLargeCoverArt(thumb);
                                // to create a folder.jpg from it
                                if (MediaPortal.Util.Utils.FileExistsInCache(thumb))// && _createMissingFolderThumbs)
                                {
                                    try
                                    {
                                        string folderjpg = MediaPortal.Util.Utils.GetFolderThumbForDir(strPath);
                                        MediaPortal.Util.Utils.FileDelete(folderjpg);
                                        File.Copy(thumb, folderjpg);
                                    }
                                    catch (Exception) { }
                                }
                            }
                        }
                        else
                        {
                            bDisplayErr = true;
                        }
                    }
                    else
                    {
                        bDisplayErr = true;
                    }
                }

                if (null != dlgProgress)
                {
                    dlgProgress.Close();
                }

                if (!bDisplayErr)
                {
                    MediaPortal.GUI.Music.GUIMusicInfo pDlgAlbumInfo = (MediaPortal.GUI.Music.GUIMusicInfo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_MUSIC_INFO);
                    if (null != pDlgAlbumInfo)
                    {
                        pDlgAlbumInfo.Album = albumInfo;
                        pDlgAlbumInfo.Tag = tag;

                        pDlgAlbumInfo.DoModal(parentWindowID);
                        if (pDlgAlbumInfo.NeedsRefresh)
                        {
                            m_db.DeleteAlbumInfo(albumName, artistName);
                            ShowAlbumInfo(isFolder, artistName, albumName, strPath, tag);
                            return;
                        }
                    }
                    else
                    {
                        if (null != dlgProgress)
                        {
                            dlgProgress.Close();
                        }
                        if (null != pDlgOK)
                        {
                            pDlgOK.SetHeading(702);
                            pDlgOK.SetLine(1, 702);
                            pDlgOK.SetLine(2, string.Empty);
                            pDlgOK.DoModal(GUIWindowManager.ActiveWindow);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("ShowAlbumInfo: " + ex.ToString());
            }
        }

        protected void ShowAlbumInfo(bool isFolder, string artistName, string albumName, string strPath, MusicTag tag)
        {
            ShowAlbumInfo(GUIWindowManager.ActiveWindow, isFolder, artistName, albumName, strPath, tag);
        }

        protected virtual void ShowArtistInfo(string artistName, string albumName)
        {
            try
            {
                GUIDialogOK pDlgOK = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
                GUIDialogProgress dlgProgress =
                  (GUIDialogProgress)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_PROGRESS);

                bool bSaveDb = true;
                bool bDisplayErr = false;

                ArtistInfo artist = new ArtistInfo();
                MusicArtistInfo artistInfo = new MusicArtistInfo();
                if (m_db.GetArtistInfo(artistName, ref artist))
                {
                    artistInfo.Set(artist);
                }
                else
                {
                    if (null != pDlgOK && !Win32API.IsConnectedToInternet())
                    {
                        pDlgOK.SetHeading(703);
                        pDlgOK.SetLine(1, 703);
                        pDlgOK.SetLine(2, string.Empty);
                        pDlgOK.DoModal(GUIWindowManager.ActiveWindow);
                        return;
                    }

                    // show dialog box indicating we're searching the artist
                    if (dlgProgress != null)
                    {
                        dlgProgress.Reset();
                        dlgProgress.SetHeading(320);
                        dlgProgress.SetLine(1, artistName);
                        dlgProgress.SetLine(2, string.Empty);
                        dlgProgress.SetPercentage(0);
                        dlgProgress.StartModal(GUIWindowManager.ActiveWindow);
                        dlgProgress.Progress();
                        dlgProgress.ShowProgressBar(true);
                    }

                    // find artist info
                    AllmusicSiteScraper scraper = new AllmusicSiteScraper();
                    if (scraper.FindInfo(AllmusicSiteScraper.SearchBy.Artists, artistName))
                    {
                        // did we find at least 1 artist?
                        if (scraper.IsMultiple())
                        {
                            // let user choose one
                            int iSelectedArtist = 0;
                            string szText = GUILocalizeStrings.Get(181);
                            GUIDialogSelect pDlg = (GUIDialogSelect)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_SELECT);
                            if (null != pDlg)
                            {
                                pDlg.Reset();
                                pDlg.SetHeading(szText);
                                foreach (string selectedArtist in scraper.GetItemsFound())
                                {
                                    pDlg.Add(selectedArtist);
                                }
                                pDlg.DoModal(GUIWindowManager.ActiveWindow);

                                // and wait till user selects one
                                iSelectedArtist = pDlg.SelectedLabel;
                                if (iSelectedArtist < 0)
                                {
                                    return;
                                }
                            }

                            // ok, now show dialog we're downloading the artist info
                            if (null != dlgProgress)
                            {
                                dlgProgress.Reset();
                                dlgProgress.SetHeading(320);
                                dlgProgress.SetLine(1, artistName);
                                dlgProgress.SetLine(2, string.Empty);
                                dlgProgress.SetPercentage(40);
                                dlgProgress.StartModal(GUIWindowManager.ActiveWindow);
                                dlgProgress.ShowProgressBar(true);
                                dlgProgress.Progress();
                            }

                            // download the artist info
                            if (!scraper.FindInfoByIndex(iSelectedArtist))
                            {
                                if (null != dlgProgress)
                                {
                                    dlgProgress.Close();
                                }
                                return;
                            }
                        }

                        if (null != dlgProgress)
                        {
                            dlgProgress.SetPercentage(60);
                            dlgProgress.Progress();
                        }

                        // Now we have either a Single hit or a selected Artist
                        // Parse it
                        if (artistInfo.Parse(scraper.GetHtmlContent()))
                        {
                            // if the artist selected from allmusic.com does not match
                            // the one from the file, override the one from the allmusic
                            // with the one from the file so the info is correct in the
                            // database...
                            if (!artistInfo.Artist.Equals(artistName))
                            {
                                artistInfo.Artist = artistName;
                            }

                            if (bSaveDb)
                            {
                                // save to database
                                m_db.AddArtistInfo(artistInfo.Get());
                            }

                            if (null != dlgProgress)
                            {
                                dlgProgress.SetPercentage(100);
                                dlgProgress.Progress();
                                dlgProgress.Close();
                                dlgProgress = null;
                            }
                        }
                        else
                        {
                            bDisplayErr = true;
                        }
                    }
                    else
                    {
                        bDisplayErr = true;
                    }
                }

                if (null != dlgProgress)
                {
                    dlgProgress.Close();
                }

                if (!bDisplayErr)
                {
                    // ok, show Artist info                    
                    MediaPortal.GUI.Music.GUIMusicArtistInfo pDlgArtistInfo =
                      (MediaPortal.GUI.Music.GUIMusicArtistInfo)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_ARTIST_INFO);
                    if (null != pDlgArtistInfo)
                    {
                        pDlgArtistInfo.Artist = artistInfo;
                        pDlgArtistInfo.DoModal(GUIWindowManager.ActiveWindow);

                        if (pDlgArtistInfo.NeedsRefresh)
                        {
                            m_db.DeleteArtistInfo(artistInfo.Artist);
                            ShowArtistInfo(artistName, albumName);
                            return;
                        }
                    }
                }
                else
                {
                    if (null != dlgProgress)
                    {
                        dlgProgress.Close();
                    }
                    if (null != pDlgOK)
                    {
                        pDlgOK.SetHeading(702);
                        pDlgOK.SetLine(1, 702);
                        pDlgOK.SetLine(2, string.Empty);
                        pDlgOK.DoModal(GUIWindowManager.ActiveWindow);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("ShowArtistInfo: " + ex.ToString());
            }
        }

        /// <summary>
        /// Returns last music track info added to MP music database.
        /// </summary>
        /// <returns>Hashtable containg artist names</returns>
        internal LatestMediaHandler.LatestsCollection GetLatestMusic(bool _onStartUp, string type)
        {
            LatestsCollection result = new LatestsCollection();
            int x = 0;
            try
            {
                GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                GUIControl gc = gw.GetControl(919199970);
                facade = gc as GUIFacadeControl;
                if (facade != null)
                {
                    facade.Clear();
                }
                if (al != null)
                {
                    al.Clear();
                }
                string sqlQuery = string.Empty;
                if (type.Equals("Latest Added Music"))
                {
                    sqlQuery = "select distinct strAlbumArtist, strAlbum, dateAdded, strGenre, strPath from tracks order by dateAdded desc limit 50;";
                }
                else if (type.Equals("Most Played Music"))
                {
                    sqlQuery = "select distinct strAlbumArtist, strAlbum, dateAdded, strGenre, strPath from tracks order by iTimesPlayed desc limit 50;";
                }
                else if (type.Equals("Latest Played Music"))
                {
                    sqlQuery = "select distinct strAlbumArtist, strAlbum, dateAdded, strGenre, strPath from tracks order by dateLastPlayed desc limit 50;";
                }
                List<Song> songInfo = new List<Song>();
                m_db.GetSongsByFilter(sqlQuery, out songInfo, "tracks");
                Hashtable ht = new Hashtable();
                string key = string.Empty;
                latestMusicAlbums = new Hashtable();
                int i0 = 1;
                foreach (Song mySong in songInfo)
                {
                    string fanart = mySong.AlbumArtist;
                    string album = mySong.Album;
                    string sPath = mySong.FileName;
                    if (sPath != null && sPath.Length > 0)// && sPath.IndexOf("\\") > 0)
                    {
                        sPath = Path.GetDirectoryName(sPath);
                    }
                    string dateAdded = string.Empty;
                    try
                    {
                        dateAdded = String.Format("{0:" + LatestMediaHandlerSetup.DateFormat + "}", mySong.DateTimeModified);
                    }
                    catch
                    { }
                    //string dateAdded = mySong.DateTimeModified.ToString(LatestMediaHandlerSetup.DateFormat, CultureInfo.CurrentCulture);
                    if (album == null || album.Trim().Length == 0)
                    {
                        album = " ";
                    }

                    key = fanart + "#" + album;
                    if (!ht.Contains(key))
                    {
                        //Get Fanart
                        string CurrSelectedMusic = String.Empty;
                        CurrSelectedMusic = String.Empty;
                        string sFilename1 = "";
                        string sFilename2 = "";
                        try
                        {
                            Hashtable ht2 = UtilsFanartHandler.GetMusicFanartForLatest(mySong.AlbumArtist);
                            if (ht2 == null || ht2.Count < 1 && !_onStartUp)
                            {
                                UtilsFanartHandler.ScrapeFanartAndThumb(mySong.AlbumArtist, mySong.Album);
                                ht2 = UtilsFanartHandler.GetMusicFanartForLatest(mySong.AlbumArtist);
                            }
                            
                            if (ht2 == null || ht2.Count < 1)
                            {
                                if (!artistsWithImageMissing.Contains(UtilsFanartHandler.GetFHArtistName(mySong.AlbumArtist)))
                                {
                                    artistsWithImageMissing.Add(UtilsFanartHandler.GetFHArtistName(mySong.AlbumArtist), UtilsFanartHandler.GetFHArtistName(mySong.AlbumArtist));
                                }
                            }
                            IDictionaryEnumerator _enumerator = ht2.GetEnumerator();

                            int i = 0;
                            while (_enumerator.MoveNext())
                            {
                                if (i == 0)
                                {
                                    sFilename1 = _enumerator.Value.ToString();
                                }
                                if (i == 1)
                                {
                                    sFilename2 = _enumerator.Value.ToString();
                                }
                                i++;
                            }
                        }
                        catch
                        { }

                        string thumb = string.Empty;
                        string sArtist = fanart;
                        thumb = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MusicAlbum, sArtist + "-" + mySong.Album);
                        if (thumb == null || thumb.Length < 1 || !File.Exists(thumb))
                        {
                            if (sArtist != null && sArtist.Length > 0 && sArtist.IndexOf("|") > 0)
                            {
                                string[] sArtists = sArtist.Split('|');
                                foreach (string _sArtist in sArtists)
                                {
                                    thumb = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MusicArtists, _sArtist.Trim());
                                    if (thumb != null && thumb.Length > 0 && File.Exists(thumb))
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                thumb = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MusicArtists, sArtist);
                            }
                        }
                        if (thumb == null || thumb.Length < 1 || !File.Exists(thumb))
                        {
                            thumb = "";
                            if (!artistsWithImageMissing.Contains(UtilsFanartHandler.GetFHArtistName(sArtist)))
                            {
                                artistsWithImageMissing.Add(UtilsFanartHandler.GetFHArtistName(sArtist), UtilsFanartHandler.GetFHArtistName(sArtist));
                            }
                        }
                        if (thumb == null || thumb.Length < 1)
                        {
                            thumb = "defaultAudioBig.png";
                        }
                        result.Add(new LatestMediaHandler.Latest(dateAdded, thumb, sFilename1, mySong.FileName, null, sArtist, mySong.Album, mySong.Genre.Replace("|", ","), null, null, null, null, null, null, null, null, null, null, null, null));
                        ht.Add(key, key);                        
                        latestMusicAlbums.Add(i0, sPath);
                        /*if (x < 3)
                        {                                                     
                            
                        } */
                        //if (facade != null)
                        //{
                            AddToFilmstrip(result[x], i0);
                        //}
                        x++;
                        i0++;
                    }

                    if (x == 10)
                    {
                        break;
                    }
                }                
                if (facade != null)
                {
                    facade.SelectedListItemIndex = LastFocusedId;
                    if (facade.ListLayout != null)
                    {
                        facade.CurrentLayout = GUIFacadeControl.Layout.List;
                        if (!facade.Focus)
                        {
                            facade.ListLayout.IsVisible = false;
                        }
                    }
                    else if (facade.FilmstripLayout != null)
                    {
                        facade.CurrentLayout = GUIFacadeControl.Layout.Filmstrip;
                        if (!facade.Focus)
                        {
                            facade.FilmstripLayout.IsVisible = false;
                        }

                    }
                    else if (facade.CoverFlowLayout != null)
                    {
                        facade.CurrentLayout = GUIFacadeControl.Layout.CoverFlow;
                        if (!facade.Focus)
                        {
                            facade.CoverFlowLayout.IsVisible = false;
                        }
                    }
                    if (!facade.Focus)
                    {
                        facade.Visible = false;
                    }
                }

                if (ht != null)
                {
                    ht.Clear();
                }
                ht = null;

            }
            catch (Exception ex)
            {
                logger.Error("GetLatestMusic: " + ex.ToString());
            }

            return result;
        }

        private void AddToFilmstrip(Latest latests, int x)
        {
            try
            {
                //Add to filmstrip
                GUIListItem item = new GUIListItem();
                MusicTag mt = new MusicTag();
                mt.Album = latests.Album;
                mt.Artist = latests.Artist;
                mt.AlbumArtist = latests.Artist;
                mt.FileName = latests.Title;
                try
                {
                    mt.Duration = Int32.Parse(latests.Runtime);
                }
                catch
                {
                    mt.Duration = 0;
                }
                try
                {
                    mt.Year = Int32.Parse(latests.Year);
                }
                catch
                {
                    mt.Year = 0;
                }
                try
                {
                    mt.Rating = Int32.Parse(latests.RoundedRating);
                }
                catch
                {
                    mt.Rating = 0;
                }
                item.ItemId = x;
                Utils.LoadImage(latests.Thumb, ref imagesThumbs);
                item.IconImage = latests.Thumb;
                item.IconImageBig = latests.Thumb;
                item.ThumbnailImage = latests.Thumb;
                item.Label = mt.Artist;
                item.Label2 = mt.Album;
                item.Label3 = latests.DateAdded;
                item.IsFolder = false;
                item.Path = latests.Genre;
                item.Duration = mt.Duration;
                item.MusicTag = mt;
                item.Year = mt.Year;
                item.DVDLabel = latests.Fanart;
                item.Rating = mt.Rating;
                //item.Path = latests.Summary;
                item.OnItemSelected += new GUIListItem.ItemSelectedHandler(item_OnItemSelected);
                if (facade != null)
                {
                    facade.Add(item);
                }
                al.Add(item);
                if (x == 1)
                {
                    UpdateSelectedProperties(item);
                }
            }
            catch (Exception ex)
            {
                logger.Error("AddToFilmstrip: " + ex.ToString());
            }
        }

        private void UpdateSelectedProperties(GUIListItem item)
        {
            try
            {
                if (item != null && selectedFacadeItem1 != item.ItemId)
                {
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.thumb", item.IconImageBig);
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.artist", item.Label);
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.album", item.Label2);
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.dateAdded", item.Label3);
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.genre", item.Path);
                    selectedFacadeItem1 = item.ItemId;

                    GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                    GUIControl gc = gw.GetControl(919199970);
                    facade = gc as GUIFacadeControl;
                    
                    if (facade != null)
                    {
                        lastFocusedId = facade.SelectedListItemIndex;                
                    }
                    
                }
            }
            catch (Exception ex)
            {
                logger.Error("UpdateSelectedProperties: " + ex.ToString());
            }
        }

        internal void UpdateSelectedImageProperties()
        {
            try
            {
                GUIWindow gw = GUIWindowManager.GetWindow(GUIWindowManager.ActiveWindow);
                GUIControl gc = gw.GetControl(919199970);
                facade = gc as GUIFacadeControl;
                if (facade != null && gw.GetFocusControlId() == 919199970 && facade.SelectedListItem != null)
                {
                    int _id = facade.SelectedListItem.ItemId;
                    String _image = facade.SelectedListItem.DVDLabel;
                    if (selectedFacadeItem2 != _id)
                    {
                        Utils.LoadImage(_image, ref images);
                        if (showFanart == 1)
                        {
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart1", _image);
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart1", "true");
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart2", "");
                            Thread.Sleep(1000);
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart2", "");
                            showFanart = 2;
                        }
                        else
                        {
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart2", _image);
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart2", "true");
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart1", "");
                            Thread.Sleep(1000);
                            LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart1", "");
                            showFanart = 1;
                        }
                        Utils.UnLoadImage(_image, ref images);
                        selectedFacadeItem2 = _id;
                    }
                }
                else
                {
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart1", " ");
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.fanart2", " ");
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart1", "true");
                    LatestMediaHandlerSetup.SetProperty("#latestMediaHandler.music.selected.showfanart2", "");
                    Utils.UnLoadImage(ref images);
                    showFanart = 1;
                    selectedFacadeItem2 = -1;
                    selectedFacadeItem2 = -1;
                }
            }
            catch (Exception ex)
            {
                logger.Error("UpdateSelectedImageProperties: " + ex.ToString());
            }
        }


        private void item_OnItemSelected(GUIListItem item, GUIControl parent)
        {
            UpdateSelectedProperties(item);
        }

        internal void PlayMusicAlbum(int index)
        {
            string _songFolder = latestMusicAlbums[index].ToString();
            if (Directory.Exists(_songFolder))
            {                
                LoadSongsFromFolder(_songFolder, false);
                StartPlayback();
            }
            
        }

        private void StartPlayback()
        {
            // if we got a playlist start playing it
            if (playlistPlayer.GetPlaylist(MediaPortal.Playlists.PlayListType.PLAYLIST_MUSIC_TEMP).Count > 0)
            {
                playlistPlayer.CurrentPlaylistType = MediaPortal.Playlists.PlayListType.PLAYLIST_MUSIC_TEMP;
                playlistPlayer.Reset();
                playlistPlayer.Play(0);
            }
        }

        private bool IsMusicFile(string fileName)
        {
            string supportedExtensions = MediaPortal.Util.Utils.AudioExtensionsDefault;// ".mp3,.wma,.ogg,.flac,.wav,.cda,.m4a,.m4p,.mp4,.wv,.ape,.mpc,.aif,.aiff";
            if (supportedExtensions.IndexOf(Path.GetExtension(fileName).ToLower()) > -1)
            {
                return true;
            }
            return false;
        }

        private void GetFiles(string folder, ref List<string> foundFiles, bool recursive)
        {
            try
            {
                string[] files = Directory.GetFiles(folder);
                foundFiles.AddRange(files);

                if (recursive)
                {
                    string[] subFolders = Directory.GetDirectories(folder);
                    for (int i = 0; i < subFolders.Length; ++i)
                    {
                        GetFiles(subFolders[i], ref foundFiles, recursive);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("GetFiles: " + ex.ToString());                
            }
        }

        private void LoadSongsFromFolder(string folder, bool includeSubFolders)
        {
            // clear current playlist
            playlistPlayer = MediaPortal.Playlists.PlayListPlayer.SingletonPlayer;
            //playlistPlayer.GetPlaylist(MediaPortal.Playlists.PlayListType.PLAYLIST_MUSIC).Clear();
            playlistPlayer.GetPlaylist(MediaPortal.Playlists.PlayListType.PLAYLIST_MUSIC_TEMP).Clear();
            int numSongs = 0;
            try
            {
                List<string> files = new List<string>();
                GetFiles(folder, ref files, includeSubFolders);
                foreach (string file in files)
                {
                    if (IsMusicFile(file))
                    {
                        MediaPortal.Playlists.PlayListItem item = new MediaPortal.Playlists.PlayListItem();
                        item.FileName = file;
                        item.Type = MediaPortal.Playlists.PlayListItem.PlayListItemType.Audio;
                        MusicTag tag = TagReader.ReadTag(file);
                        if (tag != null)
                        {
                            tag.Artist = MediaPortal.Util.Utils.FormatMultiItemMusicStringTrim(tag.Artist, _stripArtistPrefixes);
                            tag.AlbumArtist = MediaPortal.Util.Utils.FormatMultiItemMusicStringTrim(tag.AlbumArtist, _stripArtistPrefixes);
                            tag.Genre = MediaPortal.Util.Utils.FormatMultiItemMusicStringTrim(tag.Genre, false);
                            tag.Composer = MediaPortal.Util.Utils.FormatMultiItemMusicStringTrim(tag.Composer, _stripArtistPrefixes);
                            item.Description = tag.Title;
                            item.MusicTag = tag;
                            item.Duration = tag.Duration;
                        }
                        playlistPlayer.GetPlaylist(MediaPortal.Playlists.PlayListType.PLAYLIST_MUSIC_TEMP).Add(item);
                        numSongs++;
                    }
                }
            }
            catch //(Exception ex)
            {
                logger.Error("Error retrieving songs from folder.");
            }
        }

    }


}
